<?php
/**
 * 修复wiki 关联内容数量
 * @author tangyiwan
 */

ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;

$wikiId = 0;
if (isset($argv[1])) {
    $wikiId = $argv[1];
    $num = mObjRelateWiki::getWikiRelateCount($wikiId);
    mWiki::updateData(['relate_num' => $num], ['id' => $wikiId]);
} else {
    $page = 1;
    $pageSize = 2000;
    $wikiIds = [];
    do {
        $limit = [($page - 1) * $pageSize, $pageSize];
        $wikiIds = mWiki::select("id", [
            'status[!]' => mWiki::STATUS_DEL,
            'LIMIT' => $limit
        ]);
        foreach ($wikiIds as $wikiId) {
            var_dump('更新wikiId：' . $wikiId);
            $num = mObjRelateWiki::getWikiRelateCount($wikiId);
            mWiki::updateData(['relate_num' => $num], ['id' => $wikiId]);
        }
        $page++;
    } while ($wikiIds);
}